<?php

//include_once('Mail.php');
//include_once('Mail_Mime/mime.php');


class CollaborationController extends Core_Controller_Action {
	
    protected $_style = array('/styles/catalog.css', '/styles/other.css');	
	
	public function indexAction() {
//		$this->view->headLink ()->appendStylesheet ( "/styles/productList.css" );
		
		$sub_category_id = $this->_getParam ( 'sub_category' );
		$category_id = $this->_getParam ( 'category' );
		
		if (! is_null ( $sub_category_id )) {
			$subCategory = $this->_api->Category->find ( $sub_category_id );
			$topCategory = $this->_api->Category->find ( $subCategory->parent_id );
		} elseif (! is_null ( $category_id )) {
			$topCategory = $this->_api->Category->find ( $category_id );
			$subCategory = $this->_api->Category->getFirstSubCategory ( $topCategory );
		} else {
			$topCategory = $this->_api->Category->getFirstSubCategory ();
			$subCategory = $this->_api->Category->getFirstSubCategory ( $topCategory );
		}
		$this->view->topCategory = $topCategory;
		$this->view->topCategories = $top = $this->_api->Category->getNested ();
		
		$translationHelper = $this->view->getHelper ( 'T' );
		$form = new Api_Form_Collaboration ();
		$this->view->Api_Form_Collaboration = $form;
		$params = $this->getRequest ()->getParams ();
		if ($this->getRequest ()->isPost ()) {
			$formData = $this->_request->getPost ();
			if ($form->isValid ( $formData )) {
				$name_of_uploaded_file = $params ['image'];
				$type_of_uploaded_file = substr ( $name_of_uploaded_file, strrpos ( $name_of_uploaded_file, '.' ) + 1 );
				$size_of_uploaded_file = $_FILES ["uploaded_file"] ["size"] / 1024;
				
				//Settings
				$max_allowed_file_size = 10240;
				$allowed_extensions = array ("jpg", "jpeg", "gif", "bmp", "png", "zip", "rar" );
				if ($size_of_uploaded_file > $max_allowed_file_size) {
					$errors .= "Size of file should be less than $max_allowed_file_size";
				}
				$allowed_ext = false;
				for($i = 0; $i < sizeof ( $allowed_extensions ); $i ++) {
					if (strcasecmp ( $allowed_extensions [$i], $type_of_uploaded_file ) == 0) {
						$allowed_ext = true;
					}
				}
				if (! $allowed_ext) {
					$errors .= "The uploaded file is not supported file type. " . " Only the following file types are supported: " . implode ( ',', $allowed_extensions );
				}
				$path_of_uploaded_file = APPLICATION_PATH . '/../data/' . $name_of_uploaded_file;
				$tmp_path = $_FILES ["uploaded_file"] ["tmp_name"];
				
				if (is_uploaded_file ( $tmp_path )) {
					if (! copy ( $tmp_path, $path_of_uploaded_file )) {
						$errors .= 'error while copying the uploaded file';
					}
				}
				if (is_null ( $errors )) {
					$this->view->params = $params;
					$userName = $params ['first_name'] . ' ' . $params ['last_name'];
					$bodyHtml = $this->view->renderControl ( 'collaborationtMail' );
					$setSubject = $translationHelper->t ( "Order mail from " . $params ['first_name'] . ' ' . $params ['last_name'] );
				
		//					$this->Mail($userName, $bodyHtml, $setSubject, $path_of_uploaded_file);
				} else {
//					var_dump ( $errors );
//					die ();
				}
			}
		}
	}
	
	private function Mail($userName, $bodyHtml, $setSubject, $path_of_uploaded_file) {
		$mail = new Zend_Mail ( 'UTF-8', Zend_Mime::ENCODING_8BIT ); //new Mail_mime();
		$mail->addAttachment ( $path_of_uploaded_file );
		$mail->setBodyHtml ( $bodyHtml );
		$mail->setFrom ( $userName );
		$mail->addTo ( "office@suvenir.com.ua" );
		$mail->setSubject ( $setSubject );
	
		//     	$mail->send();
	}
	
	private function getFormErrorMessages(Api_Form_Collaboration $form) {
		$messages = array ();
		foreach ( $form->getMessages () as $fieldName => $errors ) {
			$errText = '';
			foreach ( $errors as $errorTitle => $errorValue ) {
				$errText .= $errorValue . '<br/>';
			}
			$messages [$fieldName] = rtrim ( $errText, '<br/>' );
		}
		return $messages;
	}
}